package com.aidong.service;

/**
 * Created by john on 16-11-3.
 */
import com.aidong.common.JobConsts;
import com.aidong.dao.wills.DicShopCodeDao;
import com.aidong.dao.wills.ReportDepositDao;
import com.aidong.dao.wills.ReportSalesByTraCodeDao;
import com.aidong.model.wills.DicShopCode;
import com.aidong.model.wills.ReportDeposit;
import com.aidong.model.wills.TbModel;
import com.aidong.util.DateUtil;
import com.aidong.util.ExportUtil;
import com.aidong.util.FileCheckUtil;
import com.aidong.util.ReportUtil;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

@Service
public class TbSummaryService extends BaseJobService {
    protected Logger log = Logger.getLogger(TbSummaryService.class);
    @Resource
    ReportSalesByTraCodeDao reportSalesByTraCodeDao;
    @Resource
    DicShopCodeDao dicShopCodeDao;
    @Resource
    ReportDepositDao reportDepositDao;
    @Resource
    ReportUtil reportUtil;

    public TbSummaryService() {
    }

    public synchronized void doJob() {
        Thread th1 = new Thread() {
            public void run() {
                try {
                    try {
                        Date e = new Date(System.currentTimeMillis() - 86400000L);
                        TbSummaryService.this.reportSalesByTraCodeDao.doReportMember(e, e);
                        TbSummaryService.this.reportSalesByTraCodeDao.doReportPT(e, e);
                        TbSummaryService.this.reportSalesByTraCodeDao.doReportDeposit(e, e);
                        TbSummaryService.this.reportSalesByTraCodeDao.doTraCodeReport(e, e);
                        TbSummaryService.this.doTbSummary();
                    } catch (Exception var5) {
                        var5.printStackTrace();
                    }

                } finally {
                    ;
                }
            }
        };
        th1.start();
    }

    private void doTbSummary() throws Exception {
        try {
            Timestamp e = DateUtil.getCurrentTimestamp();
            String cur = DateUtil.formatDate(e, DateUtil.DATE_PATTERN_1);
            String reportDate = DateUtil.addDays(cur, -1);
            String curDate = cur.split(" ", -1)[0];
            String curTime = cur.split(" ", -1)[1];
            HashMap commonData = new HashMap();
            commonData.put("date", curDate);
            commonData.put("time", curTime);
            Map data = this.createAllData(false, "");
            data.putAll(commonData);
            data.put("shopName", "总表");
            String fileName = reportDate + "_" + JobConsts.TEMPLATE_NAME_TBSUMMARY + "(ALL)" + JobConsts.EXCEL_SUFFIX;
            String fileChName = reportDate + "_" + JobConsts.CH_NAME_TBSUMMARY + "(总店)" + JobConsts.EXCEL_SUFFIX;
            String filePath = this.getDownloadPath() + reportDate + "\\" + JobConsts.TEMPLATE_NAME_TBSUMMARY;
            String fileDbPath = reportDate + "\\" + JobConsts.TEMPLATE_NAME_TBSUMMARY;
            FileCheckUtil.checkFilePath(filePath);
            ExportUtil.exportExcel(filePath, fileName, this.getTemplatePath(), JobConsts.TEMPLATE_NAME_TBSUMMARY + JobConsts.EXCEL_SUFFIX, data);
            this.reportUtil.addReportFile("01", fileName, fileChName, fileDbPath, reportDate, "ALL");
            List shopLst = this.dicShopCodeDao.getAllShopList();
            Iterator i$ = shopLst.iterator();

            while(i$.hasNext()) {
                DicShopCode shop = (DicShopCode)i$.next();
                Map shopData = this.createShopData(shop.getCode(), false, "");
                shopData.putAll(commonData);
                shopData.put("shopName", shop.getChName());
                fileName = reportDate + "_" + JobConsts.TEMPLATE_NAME_TBSUMMARY + "(" + shop.getCode() + ")" + JobConsts.EXCEL_SUFFIX;
                fileChName = reportDate + "_" + JobConsts.CH_NAME_TBSUMMARY + "(" + shop.getChName() + ")" + JobConsts.EXCEL_SUFFIX;
                ExportUtil.exportExcel(filePath, fileName, this.getTemplatePath(), JobConsts.TEMPLATE_NAME_TBSUMMARY + JobConsts.EXCEL_SUFFIX, shopData);
                this.reportUtil.addReportFile("01", fileName, fileChName, fileDbPath, reportDate, shop.getCode());
            }
        } catch (Exception var16) {
            var16.printStackTrace();
            this.log.error("生成TB总表失败");
            return;
        }

        this.log.info("生成TB总表成功");
    }

    public void createTbSummary(String createts) throws Exception {
        try {
            Date e = DateUtil.formatDate(createts);
            String cur = DateUtil.formatDate(e, DateUtil.DATE_PATTERN_1);
            String reportDate = createts;
            String curDate = cur.split(" ", -1)[0];
            String curTime = cur.split(" ", -1)[1];
            HashMap commonData = new HashMap();
            commonData.put("date", curDate);
            commonData.put("time", curTime);
            Map data = this.createAllData(true, createts);
            data.putAll(commonData);
            data.put("shopName", "总表");
            String fileName = createts + "_" + JobConsts.TEMPLATE_NAME_TBSUMMARY + "(ALL)" + JobConsts.EXCEL_SUFFIX;
            String fileChName = createts + "_" + JobConsts.CH_NAME_TBSUMMARY + "(总店)" + JobConsts.EXCEL_SUFFIX;
            String filePath = this.getDownloadPath() + createts + "\\" + JobConsts.TEMPLATE_NAME_TBSUMMARY;
            String fileDbPath = createts + "\\" + JobConsts.TEMPLATE_NAME_TBSUMMARY;
            FileCheckUtil.checkFilePath(filePath);
            ExportUtil.exportExcel(filePath, fileName, this.getTemplatePath(), JobConsts.TEMPLATE_NAME_TBSUMMARY + JobConsts.EXCEL_SUFFIX, data);
            this.reportUtil.addReportFile("01", fileName, fileChName, fileDbPath, createts, "ALL");
            List shopLst = this.dicShopCodeDao.getAllShopList();
            Iterator i$ = shopLst.iterator();

            while(i$.hasNext()) {
                DicShopCode shop = (DicShopCode)i$.next();
                Map shopData = this.createShopData(shop.getCode(), true, createts);
                shopData.putAll(commonData);
                shopData.put("shopName", shop.getChName());
                fileName = reportDate + "_" + JobConsts.TEMPLATE_NAME_TBSUMMARY + "(" + shop.getCode() + ")" + JobConsts.EXCEL_SUFFIX;
                fileChName = reportDate + "_" + JobConsts.CH_NAME_TBSUMMARY + "(" + shop.getChName() + ")" + JobConsts.EXCEL_SUFFIX;
                ExportUtil.exportExcel(filePath, fileName, this.getTemplatePath(), JobConsts.TEMPLATE_NAME_TBSUMMARY + JobConsts.EXCEL_SUFFIX, shopData);
                this.reportUtil.addReportFile("01", fileName, fileChName, fileDbPath, reportDate, shop.getCode());
            }
        } catch (Exception var17) {
            var17.printStackTrace();
            this.log.error("生成TB总表失败");
            return;
        }

        this.log.info("生成TB总表成功");
    }

    private Map<String, Object> createAllData(boolean flg, String serDate) {
        HashMap data = new HashMap();
        List allLst;
        if(flg) {
            allLst = this.reportSalesByTraCodeDao.getTbDataByDay(this.parseDate(serDate, "yyyy-MM-dd"), (String)null);
        } else {
            allLst = this.reportSalesByTraCodeDao.getTbDataByDay(this.getYesterday(), (String)null);
        }

        ArrayList tranCodelist = new ArrayList();
        ArrayList nonRevlist = new ArrayList();
        ArrayList paymentlist = new ArrayList();
        Iterator reportDeposit = allLst.iterator();

        while(true) {
            while(reportDeposit.hasNext()) {
                TbModel po = (TbModel)reportDeposit.next();
                if(!po.getCode().startsWith("1") && !po.getCode().startsWith("2") && !po.getCode().startsWith("3")) {
                    if(po.getCode().startsWith("8")) {
                        nonRevlist.add(po);
                    } else if(po.getCode().startsWith("9")) {
                        paymentlist.add(po);
                    }
                } else {
                    tranCodelist.add(po);
                }
            }

            if(tranCodelist.size() == 0) {
                tranCodelist.add(new TbModel());
            }

            if(nonRevlist.size() == 0) {
                nonRevlist.add(new TbModel());
            }

            if(paymentlist.size() == 0) {
                paymentlist.add(new TbModel());
            }

            data.put("tranCodelist", tranCodelist);
            data.put("nonRevlist", nonRevlist);
            data.put("paymentlist", paymentlist);
            ReportDeposit reportDeposit1;
            if(flg) {
                reportDeposit1 = this.reportDepositDao.getTbDeDataAllByDay(serDate);
            } else {
                reportDeposit1 = this.reportDepositDao.getTbDeDataAll();
            }

            data.put("nmuBalanceYesterday", reportDeposit1.getNMUBalance());
            data.put("nmuCharge", reportDeposit1.getNMUCharge());
            data.put("nmuTransfer", reportDeposit1.getNMUTransfer());
            data.put("ptBalanceYesterday", reportDeposit1.getPTBalance());
            data.put("ptCharge", reportDeposit1.getPTCharge());
            data.put("ptTransfer", reportDeposit1.getPTTransfer());
            data.put("deBalanceYesterday", reportDeposit1.getOtherBalance());
            data.put("deCharge", reportDeposit1.getOtherCharge());
            data.put("deTransfer", reportDeposit1.getOtherTransfer());
            return data;
        }
    }

    private Map<String, Object> createShopData(String shopCode, boolean flg, String serDate) {
        HashMap data = new HashMap();
        List allLst;
        if(flg) {
            allLst = this.reportSalesByTraCodeDao.getTbDataByDay(this.parseDate(serDate, "yyyy-MM-dd"), shopCode);
        } else {
            allLst = this.reportSalesByTraCodeDao.getTbDataByDay(this.getYesterday(), shopCode);
        }

        ArrayList tranCodelist = new ArrayList();
        ArrayList nonRevlist = new ArrayList();
        ArrayList paymentlist = new ArrayList();
        Iterator reportDeposit = allLst.iterator();

        while(true) {
            while(reportDeposit.hasNext()) {
                TbModel po = (TbModel)reportDeposit.next();
                if(!po.getCode().startsWith("1") && !po.getCode().startsWith("2") && !po.getCode().startsWith("3")) {
                    if(po.getCode().startsWith("8")) {
                        nonRevlist.add(po);
                    } else if(po.getCode().startsWith("9")) {
                        paymentlist.add(po);
                    }
                } else {
                    tranCodelist.add(po);
                }
            }

            if(tranCodelist.size() == 0) {
                tranCodelist.add(new TbModel());
            }

            if(nonRevlist.size() == 0) {
                nonRevlist.add(new TbModel());
            }

            if(paymentlist.size() == 0) {
                paymentlist.add(new TbModel());
            }

            data.put("tranCodelist", tranCodelist);
            data.put("nonRevlist", nonRevlist);
            data.put("paymentlist", paymentlist);
            ReportDeposit reportDeposit1;
            if(flg) {
                reportDeposit1 = this.reportDepositDao.getTbDeDataByShopCodeAndDay(shopCode, serDate);
            } else {
                reportDeposit1 = this.reportDepositDao.getTbDeDataByShopCode(shopCode);
            }

            data.put("nmuBalanceYesterday", reportDeposit1.getNMUBalance());
            data.put("nmuCharge", reportDeposit1.getNMUCharge());
            data.put("nmuTransfer", reportDeposit1.getNMUTransfer());
            data.put("ptBalanceYesterday", reportDeposit1.getPTBalance());
            data.put("ptCharge", reportDeposit1.getPTCharge());
            data.put("ptTransfer", reportDeposit1.getPTTransfer());
            data.put("deBalanceYesterday", reportDeposit1.getOtherBalance());
            data.put("deCharge", reportDeposit1.getOtherCharge());
            data.put("deTransfer", reportDeposit1.getOtherTransfer());
            return data;
        }
    }
}
